package com.example.boot3.controller;


import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Repair;
import com.example.boot3.model.Violation;
import com.example.boot3.service.RepairService;
import com.example.boot3.service.ViolationService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController

public class ViolationController {
    @Autowired
    private ViolationService violationService;
    @GetMapping("violation_info")
    public JsonData getRepairList(){
        List<Violation> violationList  = violationService.getViolationList();
        return JsonData.buildSuccess(violationList);
    }

    @PostMapping("register_violation")
    public JsonData registerRepair(@RequestBody Violation violation){
        Integer result = violationService.registerViolation(violation);
        return JsonData.buildSuccess(result);
    }

    @GetMapping("violation/export")
    public void Export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=violations.xlsx");
        List<Violation> data = violationService.getViolationList();
        EasyExcel.write(response.getOutputStream(), Violation.class).sheet("违章信息").doWrite(data);
    }

}
